package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	
	import game.metalmax.config.GameConfig;
	import game.metalmax.controller.ApplicationFacade;
	import game.metalmax.ui.common.Preloader001;
	
	import showtime.framework.component.loading.GroupLoader;
	import showtime.framework.component.loading.GroupLoaderEvent;
	import showtime.framework.component.loading.NowLoading;
	import showtime.framework.manager.AssetManager;
	import showtime.framework.ui.UIComponent;
	import showtime.framework.utils.FPSProfiler;
	
	[SWF(width="640",height="480",backgroundColor="#000000")]
	public class MetalMax extends Sprite
	{		
		public var nowloading:NowLoading = null;

		public function MetalMax()
		{
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			
		}	

		private function addedToStageHandler(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			
			if (ExternalInterface.available)
			{
				ExternalInterface.addCallback("windowCloseHandler", windowCloseHandler);
			}
		
			UIComponent.initializeStage(stage);	
			stage.frameRate = GameConfig.FPS;			
						
			var loader:GroupLoader = new GroupLoader();
			loader.addURL(GameConfig.URL_MAIN_SWF);
			loader.addURL(GameConfig.URL_MAP_SWF);
			loader.addURL(GameConfig.URL_ANIMATION_SWF);
			loader.addURL(GameConfig.URL_ANIMATION_XML);
			loader.addURL(GameConfig.URL_STYLE_XML);
			loader.addURL(GameConfig.URL_DIALOG_XML);
			loader.addURL(GameConfig.URL_SERVER_XML);
			loader.addURL(GameConfig.URL_AI_XML);
			loader.addEventListener(GroupLoaderEvent.COMPLETE, completeHandler);
			loader.addEventListener(GroupLoaderEvent.PROGRESS, progressHandler);
			
			nowloading = new NowLoading(null, new Preloader001());
			
			addChild(nowloading);			
			loader.start();				
		}
		
		private function progressHandler(evt:GroupLoaderEvent):void
		{
			nowloading.update(evt);			
		}

		private function completeHandler(evt:GroupLoaderEvent):void
		{
			nowloading.destroy();
			nowloading = null;
			
			evt.target.removeEventListener(GroupLoaderEvent.COMPLETE, completeHandler);
			AssetManager.getInstance().cacheAsOBJ("animationConfig", evt.target.getXML("animationConfig"));
			AssetManager.getInstance().cacheAsOBJ("styleConfig", evt.target.getXML("styleConfig"));
			AssetManager.getInstance().cacheAsOBJ("aiConfig", evt.target.getXML("aiConfig"));
			AssetManager.getInstance().cacheAsOBJ("dialogConfig", evt.target.getXML("dialogConfig"));
			AssetManager.getInstance().cacheAsOBJ("serverConfig", evt.target.getXML("serverConfig"));	
			
			evt.target.unload();			
			
			ApplicationFacade.getInstance().sendNotification(ApplicationFacade.STARTUP, root);
			
			addChild(new FPSProfiler(0xCCCCCC, 0xFFFFFF, 0x333333, 1, 3));
		}
		
		private function windowCloseHandler():void
		{
			ApplicationFacade.getInstance().sendNotification(ApplicationFacade.SHUTDOWN);
		}
	
	}
}